<?php
namespace Controllers;
use Libraries;
class Bank extends Libraries\PController {
    private $id, $nama_akun, $no_rekening, $nama_bank, $alamat_bank, $no_tlp;
    public function __construct() {
        parent::__construct();
        $this->db = new Libraries\Mysql;
    }
    public function index() {
        if (Libraries\u::isAjaxRequest()) {
            $bank = $this->db->results("SELECT * FROM psn_bank");
            $this->outputJSON(array('success' => true, 'results' => $bank));
        } else {
            $this->redirect('home');
        }
    }
    public function create() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $this->db->results("INSERT INTO psn_bank
	           (nama_akun, no_rekening, nama_bank, alamat_bank, no_tlp) VALUES ('$this->nama_akun',
                '$this->no_rekening', '$this->nama_bank', '$this->alamat_bank', '$this->no_tlp' );");
            $id = $this->db->insertId();
            $msg = "Data berhasil di simpan dengan id $id";
            $this->outputJSON(array('success' => true, 'msg' => $msg));
        } else {
            $this->redirect('home');
        }
    }
    public function update() {
        if (Libraries\u::isAjaxRequest()) {
            $this->get_submit();
            $msg = "Data dengan id $this->id berhasil di simpan.";
            $this->db->results("UPDATE psn_bank SET nama_akun = '$this->nama_akun', no_rekening = '$this->no_rekening',
	           nama_bank = '$this->nama_bank', alamat_bank = '$this->alamat_bank', no_tlp = '$this->no_tlp' WHERE id_bank = $this->id;");
            $this->outputJSON(array('success' => true, 'msg' => $msg));
        } else {
            $this->redirect('home');
        }
    }
    private function get_submit() {
        foreach ($_POST as $key => $val) {
            $_POST[$key] = $this->request->post($key,
                    FILTER_SANITIZE_MAGIC_QUOTES);
            $_POST[$key] = $this->db->escape($_POST[$key]);
            $this->$key = $val;
        }
    }
}
